﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TasksUI
{
    public partial class SyncConfig : Form
    {
        public SyncConfig()
        {
            InitializeComponent();
        }

        public SyncConfig(TasksModel.TaskLibrary library)
            : this()
        {
            System.Xml.Serialization.XmlIncludeAttribute[] al = 
                typeof(TasksModel.SynchSettings).GetCustomAttributes(
                typeof(System.Xml.Serialization.XmlIncludeAttribute), true) as System.Xml.Serialization.XmlIncludeAttribute[];
            cbProvider.DisplayMember = "Description";
            cbProvider.ValueMember = "Name";
            foreach (var a in al)
            {
                TasksModel.SynchSettings ss = Activator.CreateInstance(a.Type) as TasksModel.SynchSettings;
                TasksModel.TaskStatus ts = new TasksModel.TaskStatus();
                ts.Name = a.Type.AssemblyQualifiedName;
                ts.Description = ss.ProviderName;
                cbProvider.Items.Add(ts);
            }
        }

        public static void Show(TasksModel.TaskLibrary library)
        {
            using (SyncConfig f = new SyncConfig(library))
            {
                if (f.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                {
                    string type = ((TasksModel.TaskStatus)f.cbProvider.SelectedItem).Name;
                    Type t = Type.GetType(type);
                    library.SynchSettings = Activator.CreateInstance(t) as TasksModel.SynchSettings;
                    library.SynchSettings.Mode = TasksModel.SynchSettings.SynchDirection.TwoWay;
                }
            }
        }

        private void bApply_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void cbProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            bApply.Enabled = cbProvider.SelectedIndex >= 0;
        }
    }
}